# 常见异常：
# AssertionError : 断言失败
# AttributeError : 尝试访问未知的对象属性
# IndexError     : 索引超出序列的范围
# KeyError       : 字典中查找一个不存在的关键字
# NameError      : 访问一个不存在的变量  name ‘’ is not defined
# OSError        : 操作系统产生的异常  打开文件时报 FileNotFoundError 就是OSError的子类
# TypeError      : 不通类型间的无效操作
# SyntaxError    : Python语法错误
# ZeroDivisionError : 除数为0

# try-except 语句 捕获异常
# try:
#     sum = 1 + '1'
#     f = open('upload.txt','r',encoding='utf-8')
#     print(f.read())
#     f.close()
# except OSError as reason:
#     print("文件打开异常：", str(reason))
# except TypeError as reason:
#     print("类型错误：",str(reason))

# 对多个异常同一处理
# try:
#     sum = 1 + '1'
#     f = open('upload.txt','r',encoding='utf-8')
#     print(f.read())
#     f.close()
# except (OSError,TypeError) as reason:
#     print("程序异常：", str(reason))

# try - finally  raise-抛出异常
# try:
#     f = open('upload.txt','r',encoding='utf-8')
#     print(f.read())
#     sum = 1 + '1'
# except (OSError,TypeError) as reason:
#     print("程序异常：", str(reason))
#     # 抛出异常
#     raise ZeroDivisionError("除数不能为0")
# finally:
#     f.close()

# with 语句 不用担心文件打开后忘记关闭得问题，with会自动关闭文件

try:
    with open('d:\\upload.txt','r',encoding='utf-8') as f :
        # print(f.read())
        for each_line in f:
            print("--->",each_line)
except (OSError,TypeError) as reason:
    print("程序异常：", str(reason))
    # 抛出异常
    raise ZeroDivisionError("除数不能为0")
